<?php
use  Doctrine\DBAL\Event\Listeners\MysqlSessionInit;

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initExtjs()
	{
		$view = new Zend_View();
		$view->addHelperPath('ZendX/Extjs/View/Helper/', 'ZendX_Extjs_View_Helper');
 
		$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
		$viewRenderer->setView($view);
		Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
	}
		
	protected function _initDoctrine()
	{	
		require_once "Doctrine/ORM/Tools/Setup.php";
		Doctrine\ORM\Tools\Setup::registerAutoloadDirectory(APPLICATION_PATH . '/../library');

		//Doctrine\ORM\Tools\Setup::registerAutoloadDirectory(APPLICATION_PATH . '/../models');
		if (Zend_Auth::getInstance()->hasIdentity()) {
			Zend_Loader_Autoloader::getInstance()->registerNamespace(Zend_Auth::getInstance()->getIdentity()->company);
		}
		
		$isDevMode = (APPLICATION_ENV == 'development');
		//$config = Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array(__DIR__."/models"), $isDevMode);
		$config = Doctrine\ORM\Tools\Setup::createXMLMetadataConfiguration(array(APPLICATION_PATH."/../xmldriver"), $isDevMode);

		if ($isDevMode) {
			//$config->setSQLLogger(new Doctrine\DBAL\Logging\EchoSQLLogger);
		}
		
		$connectionSettings = $this->getPluginResource('db')->getOptions();   
        $conn = array(
            'driver'    => $connectionSettings['adapter'],
            'user'      => $connectionSettings['params']['username'],
            'password'  => $connectionSettings['params']['password'],
            'dbname'    => $connectionSettings['params']['dbname'],
            'host'      => $connectionSettings['params']['host']
        );
        		
		$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
		$entityManager->getEventManager()->addEventSubscriber(new MysqlSessionInit('utf8', 'utf8_unicode_ci')); 

		$conn = $entityManager->getConnection();
		$conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
		
		$registry = Zend_Registry::getInstance();
		$registry->entityManager = $entityManager;
		
		return $entityManager;
	}
	

}

